package com.lantosec.hmrp.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.util.MonitorUtils;
import com.lantosec.hmrp.modules.monitor.entity.EvaluationEntity;
import com.lantosec.hmrp.modules.monitor.entity.EvaluationEventEntity;
import com.lantosec.hmrp.modules.monitor.entity.EvaluationItemEntity;
import com.lantosec.hmrp.modules.monitor.mapper.MonitorEvaluationMapper;
import com.lantosec.hmrp.modules.monitor.service.IMonitorEvaluationService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Primary
@Log4j2
@SuppressWarnings("all")
public class MonitorEvaluationServiceImpl extends ServiceImpl<MonitorEvaluationMapper, EvaluationEntity> implements IMonitorEvaluationService {
    @Override
    public List<EvaluationEntity> getDeptRank(String month, String year) {
        List<EvaluationEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        param = baseMapper.getDeptRank(month, year, startTime, endTime);
        return param;
    }

    @Override
    public List<EvaluationEntity> getPersonalRank(String month, String year) {
        List<EvaluationEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        param = baseMapper.getPersonalRank(month, year, startTime, endTime);
        return param;
    }

    @Override
    public Map<String, Integer> getDeclareTotal(String userId) {
        Map<String, Integer> map = new HashMap<>();
        //个人奖惩申报
        Integer userTotal = baseMapper.getUserTotal(userId);
        //科室奖惩申报
        Integer deptTotal = baseMapper.getDeptTotal(userId);
        map.put("userTotal", userTotal);
        map.put("deptTotal", deptTotal);
        return map;
    }

    @Override
    public List<EvaluationEntity> getEvaluationList(String userId) {
        List<EvaluationEntity> evaluationList = baseMapper.getEvaluationList(userId);
        if(evaluationList.size() > 0 && null != evaluationList){
            return evaluationList;
        }
       return new ArrayList<>();
    }


    /**
     * @date: 2020-06-29 22:26
     * @instructions: 工作台医德医风待审核数字汇总
     * @param:
     * @return:
     */
    @Override
    public Map<String, Integer> getMoralityTotal(String userId) {
        //部门待审核
        Integer deptTotal = baseMapper.getDeptNotExamineTotal(userId);
        //党支部待审核列表
        Integer partyTotal = baseMapper.getPartyBranchExamineTotal(userId);
        //考评委员会待审核列表
        Integer appraisalTotal = baseMapper.getAppraisalExamineTotal(userId);
        Map<String, Integer> map = new HashedMap();
        map.put("deptTotal", deptTotal);
        map.put("partyTotal", partyTotal);
        map.put("appraisalTotal", appraisalTotal);
        return map;
    }


    /**
     * @date: 2020-06-29 22:46
     * @instructions: 工作台医德医风待办列表
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationEntity> getMoralityBackLog(String userId) {
        return baseMapper.getMoralityBackLog(userId);
    }


    /**
     * @date: 2020-06-29 22:50
     * @instructions: 内部满意度待办列表
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationEntity> getInternalList(String userId) {
        return baseMapper.getInternalList(userId);
    }


    /**
     * @date: 2020-07-02 15:46
     * @instructions: 科室考评项目明细
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationItemEntity> getItemDetails(String month, String year, String deptId) {
        List<EvaluationEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        return baseMapper.getItemDetails(month, year, deptId, startTime, endTime);
    }

    /**
     * @date: 2020-07-02 16:14
     * @instructions:科室考评项目事件明细
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationEventEntity> getEventDetails(String month, String year, String evalItemId, String deptId) {
        List<EvaluationEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        return baseMapper.getEventDetails(month, year, evalItemId, startTime, endTime, deptId);
    }


    /**
     * @date: 2020-07-02 16:35
     * @instructions:个人考评项目明细
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationItemEntity> getPersonalItemDetails(String month, String year, String userId) {
        List<EvaluationItemEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        return baseMapper.getPersonalItemDetails(month, year, startTime, endTime, userId);
    }


    /**
     * @date: 2020-07-02 16:45
     * @instructions: 个人考评项目事件明细
     * @param:
     * @return:
     */
    @Override
    public List<EvaluationEventEntity> getPersonalEventDetails(String month, String year, String evalItemId, String userId) {
        List<EvaluationEventEntity> param = null;
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            String[] monthSplit = month.split("-");
            startTime = MonitorUtils.getFirstDayOfMonth(Integer.valueOf(monthSplit[1]));
            endTime = MonitorUtils.getLastDayOfMonth(Integer.valueOf(monthSplit[1]));
        } else if (StringUtils.isNotBlank(year)) {
            startTime = year;
        } else {
            month = MonitorUtils.getLastMonth();
            startTime = MonitorUtils.lastMonthFirstDay();
            endTime = MonitorUtils.lastMonthLastDay();
        }
        return baseMapper.getPersonalEventDetails(month, year, startTime, endTime, userId, evalItemId);
    }
}
